class Solution:
    def myAtoi(self, s: str) -> int:
        if not s:
            return 0
        n = len(s)
        if n == 0:
            return 0
        i = 0
        while s[i] == ' ':
            i += 1
            # only contains blank space
            if i == n:
                return 0
        sign = -1 if s[i] == '-' else 1
        if s[i] in ['-', '+']:
            i += 1
        res, flag = 0, (2 ** 31 - 1) // 10
        while i < n:
            # not a number, exit the loop
            if not s[i].isdigit():
                break
            c = int(s[i])
            # if overflows
            if res > flag or (res == flag and c > 7):
                return 2 ** 31 - 1 if sign > 0 else -2 ** 31
            res = res * 10 + c
            i += 1
        return sign * res
